﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.NonlinearEquationSolvers
{
    /// <summary>
    /// This class provides the finding of roots of a polynomial by using the Halley method.
    /// </summary>
    [Serializable]
    public class HalleyRootFinder : AbstractDerivativeNeedRootFinder
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HalleyRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public HalleyRootFinder(Polynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HalleyRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public HalleyRootFinder(SimplePolynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Find one root of the polynomial by using the Halley method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The start value of the approximation.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x)
        {
            return this.FindRoots(x, 1e-15, 1000);
        }

        /// <summary>
        /// Find one root of the polynomial by using the Halley method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The start value of the approximation.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x, int iterations)
        {
            return this.FindRoots(x, 1e-15, iterations);
        }

        /// <summary>
        /// Find one root of the polynomial by using the Halley method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The start value of the approximation.</param>
        /// <param name="precision">The precision of the result.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x, double precision, int iterations)
        {
            if (this.Polynomial.Degree < 2)
            {
                throw new ArgumentException("The minimum degree of the polynomial has to be two.");
            }

            double tempuri = 0;
            Polynomial firstDerivation = this.Polynomial.Derivative();
            Polynomial secondDerivation = firstDerivation.Derivative();

            for (int i = 0; i < iterations; i++)
            {
                double originalValue = this.Polynomial.SolveAt(x);
                double firstDerivationValue = firstDerivation.SolveAt(x);
                double secondDerivationValue = secondDerivation.SolveAt(x);

                tempuri = x -
                          ((2 * originalValue * firstDerivationValue) /
                           (2 * Math.Pow(firstDerivationValue, 2) - originalValue * secondDerivationValue));

                if (Math.Abs(tempuri - x) < precision)
                {
                    this.NeededIterations = i;
                    this.PrecisionError = false;
                    this.RelativeError = Math.Abs(tempuri - x);

                    return tempuri;
                }

                x = tempuri;
            }

            this.PrecisionError = true;
            this.NeededIterations = iterations;
            this.RelativeError = Math.Abs(tempuri - x);

            return x;
        }
    }
}